home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.5 Applications 2004 May / SGI IRIX 6.5 Applications 2004 May.iso / dist / java3d.idb / usr / demos / java / j3d / programs / examples / FPSCounter / FPSCounterDemo.java.z / FPSCounterDemo.java
Encoding:
Java Source  |  2003-08-08  |  7.9 KB  |  196 lines

  1. /*
  2.  *    @(#)FPSCounterDemo.java 1.3 02/10/21 13:38:59
  3.  *
  4.  * Copyright (c) 1996-2002 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions
  8.  * are met:
  9.  *
  10.  * - Redistributions of source code must retain the above copyright
  11.  *   notice, this list of conditions and the following disclaimer.
  12.  *
  13.  * - Redistribution in binary form must reproduce the above copyright
  14.  *   notice, this list of conditions and the following disclaimer in
  15.  *   the documentation and/or other materials provided with the
  16.  *   distribution.
  17.  *
  18.  * Neither the name of Sun Microsystems, Inc. or the names of
  19.  * contributors may be used to endorse or promote products derived
  20.  * from this software without specific prior written permission.
  21.  *
  22.  * This software is provided "AS IS," without a warranty of any
  23.  * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
  24.  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
  25.  * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
  26.  * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
  27.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  28.  * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
  29.  * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
  30.  * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
  31.  * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
  32.  * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
  33.  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  34.  *
  35.  * You acknowledge that Software is not designed,licensed or intended
  36.  * for use in the design, construction, operation or maintenance of
  37.  * any nuclear facility.
  38.  */
  39.  
  40. import java.applet.Applet;
  41. import java.awt.BorderLayout;
  42. import java.awt.event.*;
  43. import java.awt.GraphicsConfiguration;
  44. import com.sun.j3d.utils.applet.JMainFrame;
  45. import com.sun.j3d.utils.geometry.ColorCube;
  46. import com.sun.j3d.utils.universe.*;
  47. import javax.media.j3d.*;
  48. import javax.vecmath.*;
  49. import javax.swing.JOptionPane;
  50.  
  51. /** This program demonstrates the use of the frames per second counter. 
  52.   * The program displays a rotating cube and sets up the FPSCounter to compute 
  53.   * the frame rate. The FPSCounter is set up with default values: 
  54.   *     - run indefinitely 
  55.   *    - 2 sec. warmup time 
  56.   *    - display average frame rate every fifth sampling interval. 
  57.   * The default values can be changed through the command line 
  58.   * arguments. Use FPSCounterDemo -h for help on the various arguments.
  59.   */
  60.  
  61. public class FPSCounterDemo extends Applet {
  62.   private SimpleUniverse u = null;
  63.   private FPSCounter fpsCounter = new FPSCounter(); 
  64.  
  65.   BranchGroup createSceneGraph() {
  66.     // Create the root of the branch graph
  67.     BranchGroup objRoot = new BranchGroup();
  68.  
  69.     // Create the TransformGroup node and initialize it to the
  70.     // identity. Enable the TRANSFORM_WRITE capability so that
  71.     // our behavior code can modify it at run time. Add it to
  72.     // the root of the subgraph.
  73.     TransformGroup objTrans = new TransformGroup();
  74.     objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
  75.     objRoot.addChild(objTrans);
  76.  
  77.     // Create a simple Shape3D node; add it to the scene graph.
  78.     objTrans.addChild(new ColorCube(0.4));
  79.  
  80.     // Create a new Behavior object that will perform the
  81.     // desired operation on the specified transform and add
  82.     // it into the scene graph.
  83.     Transform3D yAxis = new Transform3D();
  84.     Alpha rotationAlpha = new Alpha(-1, 4000);
  85.  
  86.     RotationInterpolator rotator =
  87.         new RotationInterpolator(rotationAlpha, objTrans, yAxis,
  88.                  0.0f, (float) Math.PI*2.0f);
  89.     BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0), 
  90.                         100.0);
  91.     rotator.setSchedulingBounds(bounds);
  92.     objRoot.addChild(rotator);
  93.  
  94.     // Create the Framecounter behavior
  95.     fpsCounter.setSchedulingBounds(bounds);
  96.     objRoot.addChild(fpsCounter);
  97.  
  98.     return objRoot;
  99.   }
  100.  
  101.  
  102.   public FPSCounterDemo(String args[]) {
  103.   }
  104.  
  105.   public FPSCounterDemo() {
  106.   }
  107.  
  108.   public void init() {
  109.       setLayout(new BorderLayout());
  110.       GraphicsConfiguration config =
  111.       SimpleUniverse.getPreferredConfiguration();
  112.       
  113.       Canvas3D c = new Canvas3D(config);
  114.       add("Center", c);
  115.       
  116.       // Create a simple scene and attach it to the virtual universe
  117.       BranchGroup scene = createSceneGraph();
  118.       
  119.       // Parse the command line to set the various parameters
  120.  
  121.       // Have Java 3D perform optimizations on this scene graph.
  122.       scene.compile();
  123.       u = new SimpleUniverse(c);
  124.       
  125.       // This will move the ViewPlatform back a bit so the
  126.       // objects in the scene can be viewed.
  127.       u.getViewingPlatform().setNominalViewingTransform();
  128.       
  129.       u.addBranchGraph(scene);
  130.  
  131.       JOptionPane.showMessageDialog(this, "\nThis program measures the number of frames rendered per second.\nNote that the frame rate is limited by the refresh rate of the monitor.\nTo get the true frame rate you need to disable vertical retrace.\n\nOn Windows(tm) you do this through the Control Panel.\n\nOn Unix set the environment variable OGL_NO_VBLANK\n(i.e. type \"setenv OGL_NO_VBLANK\" at the command prompt)", "Frame Counter", JOptionPane.INFORMATION_MESSAGE);
  132.   }
  133.   
  134.   public void destroy() {
  135.       u.cleanup();
  136.   }
  137.   
  138.   //
  139.   // The following allows FPSCounterDemo to be run as an application
  140.   // as well as an applet
  141.   //
  142.   public static void main(String[] args) {
  143.       FPSCounterDemo fp = new FPSCounterDemo();
  144.       fp.parseArgs(args);
  145.       JMainFrame frame = new JMainFrame(fp, 256, 256);
  146.   }
  147.   
  148.   /** Parses the commandline for the various switches to set the FPSCounter 
  149.     * variables.
  150.    * All arguments are of the form <i>-name value</i>. 
  151.    * All -name arguments can be shortened to one character. All the value 
  152.    * arguments take a number. The arguments accepted are :
  153.    * <ul>
  154.    * <li>warmupTime : Specifies amount of time the FPSCounter should wait 
  155.    * for the HotSpot<sup><font size="-2">TM</font></sup> VM to perform 
  156.    * initial optimizations. Specified in milliseconds<br>
  157.    * <li>loopCount  : Specifies the number of sampling intervals over which 
  158.    * the FPSCounter should calculate the aggregate and average frame rate. 
  159.    * Specified as a count. <br>
  160.    * <li>maxLoops   : Specifies that the FPSCounter should run for only 
  161.    * these many sampling intervals. Specified as number. If this argument 
  162.    * is not specified, the FPSCounter runs indefinitely. <br>
  163.    * <li>help       : Prints the accepted arguments. <br>
  164.    * </ul>
  165.    */
  166.  
  167.   private void parseArgs(String args[]) {
  168.       for(int i = 0; i < args.length; i++) {
  169.       if(args[i].startsWith("-")) {
  170.           if(args[i].startsWith("w", 1)) {
  171.           i++;
  172.           System.out.println("Warmup time : " + args[i]);
  173.           int w = new Integer(args[i]).intValue();
  174.           fpsCounter.setWarmupTime(w);
  175.           }
  176.           else if(args[i].startsWith("l", 1)) {
  177.           i++;
  178.           System.out.println("Loop count : " + args[i]);
  179.           int l = new Integer(args[i]).intValue();
  180.           fpsCounter.setLoopCount(l);
  181.           }
  182.           else if(args[i].startsWith("m", 1)) {
  183.           i++;
  184.           System.out.println("Max Loop Count : " + args[i]);
  185.           int m = new Integer(args[i]).intValue();
  186.           fpsCounter.setMaxLoops(m);
  187.           }    
  188.           else if(args[i].startsWith("h", 1)) {
  189.           System.out.println("Usage : FPSCounterDemo [-name value]\n All arguments are of the form -name value. All -name arguments can be shortened to one character. All the value arguments take a number. The arguments accepted are :\n  warmupTime : Specifies amount of time the FPSCounter should wait for the HotSpot(tm) VM to perform initial optimizations. Specified in milliseconds\n  loopCount : Specifies the number of sampling intervals over which the FPSCounter should calculate the aggregate and average frame rate. Specified as a count\n  maxLoops : Specifies that the FPSCounter should run for only these many sampling intervals. Specified as number. If this argument is not specified, the FPSCounter runs indefinitely.\n  help : Prints this message.");
  190.           }
  191.       }
  192.       }
  193.   }
  194.   
  195. }
  196.